/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pl.droidsonroids.gif.gifsample;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;
import ohos.global.resource.RawFileDescriptor;
import pl.droidsonroids.gif.GifDecoder;
import pl.droidsonroids.gif.InputSource;

import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * DecoderTest
 */
public class DecoderTest {
    private static final int LOOP_COUNT_0 = 0;
    private static final int FRAME_COUNT = 2;  // Number of frames in the given gif file

    GifDecoder decoder ;
    private Context mContext;
    private ComponentContainer rootLayout;
    private Image image;

    /**
     * setUp
     */
    @Before
    public void setUp() {
        mContext = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        rootLayout = (ComponentContainer) LayoutScatter.getInstance(mContext)
            .parse(ResourceTable.Layout_layout_decoder, null, false);
        image = (Image)rootLayout.findComponentById(ResourceTable.Id_decoderImageView);
        RawFileDescriptor rawFileDescriptor = null;
        try {
            rawFileDescriptor = mContext.getResourceManager()
                .getRawFileEntry("entry/resources/rawfile/anim_flag_ok_large.gif").openRawFileDescriptor();
        } catch (IOException e) {
            // Ignore
        }
        try {
            decoder = new GifDecoder(new InputSource.AssetFileDescriptorSource(rawFileDescriptor));
        } catch (IOException e) {
            // Ignore
        } catch (DataAbilityRemoteException e) {
            // Ignore
        }
    }

    /**
     * testDecoderInstance
     */
    @Test
    public void testDecoderInstance() {
        assertNotNull(decoder);
    }

    /**
     * testLoopCountNotSet
     */
    @Test
    public void testLoopCountNotSet() {
        assertEquals(decoder.getLoopCount(), LOOP_COUNT_0);
    }

    /**
     * testWidth
     */
    @Test
    public void testWidth() {
        assertTrue(decoder.getWidth() > 0);
    }

    /**
     * testHeight
     */
    @Test
    public void testHeight() {
        assertTrue(decoder.getHeight() > 0);
    }

    /**
     * testFrameDuration
     */
    @Test
    public void testFrameDuration() {
        assertTrue(decoder.getFrameDuration(1) > 0);
    }

    /**
     * testNumberOfFrames
     */
    @Test
    public void testNumberOfFrames() {
        assertEquals(decoder.getNumberOfFrames(), FRAME_COUNT);
    }
}